const saveStorageState = (storeid, state, persistence) => {
    let _state = {};
    if (!persistence.state) {
        _state = state
    } else {
        _state = persistence.state.reduce((_obj, prop) => {
            _obj[prop] = state[prop]
            return _obj
        }, {})
    }
    if (persistence.type === 'session') {
        sessionStorage.setItem(storeid, JSON.stringify(_state))
    } else localStorage.setItem(storeid, JSON.stringify(_state))
    // persistence.type === 'session' ? 
    // sessionStorage.setItem(storeid,JSON.stringify(store_state)):
    // localStorage.setItem(storeid,JSON.stringify(store_state))
}
const PersistencePlguin = ({ options, store }) => {
    console.log('options=>', options);
    console.log('store=>', store);
    console.log('persistence length=>', Object.keys(options.Persistence).length);
    if (options.Persistence && Object.keys(options.Persistence).length > 0) {
        if (options.Persistence.enabled === undefined || options.Persistence.enabled === null) {
            options.Persistence.enabled = true
        }
        if (!options.Persistence.type) {
            options.Persistence.type = 'session';
        }

    }

    store.$subscribe((mutation, state) => {
        saveStorageState(store.$id, state, options.Persistence)
    })

}

export default PersistencePlguin